//
//  AccountCell.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/1.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class AccountCell: UITableViewCell {
    
    var model : InputBillModel! {
        didSet{
            updateData()
        }
    }
    
    var isFlag = false //显示删除置顶操作
    
    let whiteView = UIView()
    fileprivate let bankImage = UIImageView() //图标
    fileprivate let bankLabel = UILabel() //银行
//    fileprivate let numberLabel = UILabel() //卡号
//    fileprivate let nameLabel = UILabel() //名字
    let repaymentBtn = UIButton() //还款
    fileprivate let payLabel = UILabel() //已还清
    
    fileprivate let amountTipLabel = UILabel()
    fileprivate let linesTipLabel = UILabel()
    fileprivate let dateTipLabel = UILabel()
    
    fileprivate let amountLabel = UILabel() //应还金额
    fileprivate let linesLabel = UILabel() //剩余额度
    fileprivate let dateLabel = UILabel() //日期
    
    let moreButton = UIButton() //更多
    let deleteButton = UIButton() //删除
    let topButton = UIButton() //置顶
    
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = UIColor.mineColor()
    
        whiteView.backgroundColor = UIColor.white
        whiteView.layer.cornerRadius = 10
        whiteView.layer.masksToBounds = true
        self.contentView.addSubview(whiteView)
        
        bankImage.image = UIImage.imageWithName("boc")
        whiteView.addSubview(bankImage)
        
        bankLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        whiteView.addSubview(bankLabel)
        
//        nameLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
//        whiteView.addSubview(nameLabel)
//
//        numberLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
//        whiteView.addSubview(numberLabel)
        
        moreButton.layer.contents = UIImage.init(named: "more_select")?.cgImage
        moreButton.addTarget(self, action: #selector(operateBill(sender:)), for: .touchUpInside)
        whiteView.addSubview(moreButton)
        
        deleteButton.setTitle(delete_title, for: .normal)
        deleteButton.setTitleColor(UIColor.red, for: .normal)
        deleteButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 12))
        deleteButton.layer.cornerRadius = RATIO_H(maxNum: 10)
        deleteButton.layer.masksToBounds = true
        deleteButton.layer.borderColor = UIColor.red.cgColor
        deleteButton.layer.borderWidth = 1
        deleteButton.isHidden = true
        whiteView.addSubview(deleteButton)
        
        topButton.setTitle(goTop, for: .normal)
        topButton.setTitleColor(UIColor.green, for: .normal)
        topButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 12))
        topButton.layer.cornerRadius = RATIO_H(maxNum: 10)
        topButton.layer.masksToBounds = true
        topButton.layer.borderColor = UIColor.green.cgColor
        topButton.layer.borderWidth = 1
        topButton.isHidden = true
        whiteView.addSubview(topButton)
        
        payLabel.text = repaymenting
        payLabel.textColor = UIColor.green
        payLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        whiteView.addSubview(payLabel)
        
        repaymentBtn.setTitle(toRepayment, for: .normal)
        repaymentBtn.backgroundColor = UIColor.red
        repaymentBtn.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        repaymentBtn.layer.cornerRadius = RATIO_H(maxNum: 10)
        repaymentBtn.layer.masksToBounds = true
        whiteView.addSubview(repaymentBtn)
        
        amountLabel.font = UIFont.boldSystemFont(ofSize: RATIO(maxNum: 16))
        whiteView.addSubview(amountLabel)
        
        linesLabel.font = UIFont.boldSystemFont(ofSize: RATIO(maxNum: 16))
        whiteView.addSubview(linesLabel)
        
        dateLabel.font = UIFont.boldSystemFont(ofSize: RATIO(maxNum: 14))
        dateLabel.textColor = UIColor.red
        whiteView.addSubview(dateLabel)
        
        amountTipLabel.text = payAmount
        amountTipLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        amountTipLabel.textColor = UIColor.gray
        whiteView.addSubview(amountTipLabel)
        
        linesTipLabel.text = remainingAmount
        linesTipLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        linesTipLabel.textColor = UIColor.gray
        whiteView.addSubview(linesTipLabel)
        
        dateTipLabel.text = dayFromRepayment
        dateTipLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        dateTipLabel.textColor = UIColor.gray
        whiteView.addSubview(dateTipLabel)
        
        whiteView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 5, left: 10, bottom: 5, right: 10))
        }
        
        bankImage.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.width.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        bankLabel.snp.makeConstraints { (make) in
            make.left.equalTo(bankImage.snp.right).offset(RATIO(maxNum: 5))
            make.centerY.equalTo(bankImage)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
//        nameLabel.snp.makeConstraints { (make) in
//            make.left.equalTo(bankLabel.snp.right).offset(RATIO(maxNum: 10))
//            make.centerY.equalTo(bankLabel)
//            make.width.lessThanOrEqualTo(100)
//            make.height.greaterThanOrEqualTo(10)
//        }
//
//        numberLabel.snp.makeConstraints { (make) in
//            make.left.equalTo(nameLabel.snp.right).offset(RATIO(maxNum: 10))
//            make.centerY.equalTo(nameLabel)
//            make.width.greaterThanOrEqualTo(10)
//            make.height.greaterThanOrEqualTo(10)
//        }
        
        moreButton.snp.makeConstraints { (make) in
            make.left.equalTo(bankLabel.snp.right).offset(RATIO(maxNum: 5))
            make.centerY.equalTo(bankLabel)
            make.width.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        deleteButton.snp.makeConstraints { (make) in
            make.left.equalTo(moreButton.snp.right).offset(RATIO(maxNum: 5))
            make.centerY.equalTo(moreButton)
            make.width.equalTo(RATIO(maxNum: 40))
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        topButton.snp.makeConstraints { (make) in
            make.left.equalTo(deleteButton.snp.right).offset(RATIO(maxNum: 5))
            make.centerY.equalTo(deleteButton)
            make.width.equalTo(deleteButton)
            make.height.equalTo(deleteButton)
        }
            
        
        repaymentBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(bankLabel)
            make.width.equalTo(RATIO(maxNum: 70))
            make.height.equalTo(RATIO_H(maxNum: 20))
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
        }
        
        payLabel.snp.makeConstraints { (make) in
            make.centerY.right.height.equalTo(repaymentBtn)
            make.width.greaterThanOrEqualTo(10)
        }
        
        linesLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 20))
            make.top.equalTo(bankLabel.snp.bottom).offset(RATIO_H(maxNum: 15))
        }
        
        amountLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.centerY.equalTo(linesLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(linesLabel)
        }
        
        dateLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.centerY.equalTo(amountLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(amountLabel)
        }
        
        linesTipLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(linesLabel.snp.bottom).offset(RATIO_H(maxNum: 5))
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(linesLabel)
        }
        
        amountTipLabel.snp.makeConstraints { (make) in
            make.left.equalTo(amountLabel)
            make.centerY.equalTo(linesTipLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(linesTipLabel)
        }
        
        dateTipLabel.snp.makeConstraints { (make) in
            make.right.equalTo(dateLabel)
            make.centerY.equalTo(amountTipLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(amountTipLabel)
        }
        
    }
    
    //MARK: 账单操作
    @objc fileprivate func operateBill(sender:UIButton) {
        
        self.isFlag = !self.isFlag
        
        if self.isFlag {
            sender.layer.contents = UIImage.init(named: "more_unselect")?.cgImage
            deleteButton.isHidden = false
            topButton.isHidden = false
        }else{
            sender.layer.contents = UIImage.init(named: "more_select")?.cgImage
            deleteButton.isHidden = true
            topButton.isHidden = true
        }
    }
    
    //MARK: 刷新数据
    fileprivate func updateData() {
        
        bankImage.image = UIImage.imageWithName(KMTool.selectBankImage(bankName: model.bank!))
        bankLabel.text = model.bank
//        nameLabel.text = model.name
//        numberLabel.text = model.number
        amountLabel.text = "¥ " + model.billLines!
        linesLabel.text = "¥ " + model.cardLines!
        dateLabel.text = model.repaymentDate?.distanceRepaymentDate()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override var canBecomeFirstResponder: Bool {
        
        return true
    }
    
    
    
}
